﻿using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Security;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace RStein.Posterous.API.Test
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class PosterousSitesTest
    {
        public const string SITE_TAG = "Počítače";
        public PosterousSitesTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;
        private IPosterousAccount m_account;
        private ITwitterAccount m_twitterAccount;
        private const string MEDIA_PATH = @"c:\Users\STEIN\Documents\Authortest.jpg";

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {                
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        
         [ClassInitialize()]
         public static void MyClassInitialize(TestContext testContext)
         {            
         }
        //
         //Use ClassCleanup to run code after all tests in a class have run
         //[ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        [TestInitialize()]
         public void MyTestInitialize()
        {
            
            m_account = PosterousApplication.Current.GetPosterousAccount("stein@tiscali.cz", "xxx");
            m_twitterAccount = PosterousApplication.Current.GetTwitterAccount("rstest2", "xxxxx");
        }
        //
        // Use TestCleanup to run code after each test has run
         [TestCleanup()]
        public void MyTestCleanup()
         {
           
         }
        //
        #endregion

        [TestMethod]
        public void Assert_Download_Sites_OK()
        {
            m_account.LoadSites();
           Assert.IsFalse(m_account.Sites.Count() == 0);
        }

        [TestMethod]
        public void Assert_Secondary_Sites_OK()
        {            
            Assert.IsFalse(m_account.Sites.Count() <= 1);
            IPosterousSite site = getSecondarySite();
            TestContext.WriteLine(site.Tags.Count().ToString());
            Assert.IsTrue(site.IsTagsLoaded);
        }

        [TestMethod]
        public void Assert_AddPost_Secondary_Site_OK()
        {
            Assert.IsFalse(m_account.Sites.Count() <= 1);
            IPosterousSite site = getSecondarySite();
            IPosterousPost newPost = site.CreatePost(DateTime.Now + " Příliš žluťoučký kůň úpěl ďábelské ódy",
                                             "Příliš žluťoučký kůň úpěl ďábelské ódy", true);

            newPost.AddTag("Všechno a nic");
            newPost.AddTag("C# omnia vincit");
            newPost.SaveChanges();
            Assert.IsTrue(newPost.Id > 0);
        }
        private IPosterousSite getSecondarySite()
        {
            return m_account.Sites.Where(st => !st.IsPrimary).First();
        }

        [TestMethod]
        public void Assert_Auth_Picture_Download_OK()
        {            
            Assert.IsFalse(m_account.PrimarySite.Posts.First().Author.AuthorPicture.Length == 0);
            using (Stream athFileStream = File.Open(MEDIA_PATH, FileMode.Create, FileAccess.Write))
            {
                m_account.PrimarySite.Posts.First().Author.AuthorPicture.CopyToStream(athFileStream);
            }
        }

        [TestMethod]
        public void Assert_Media_Download_OK()
        {            
            Assert.IsFalse(m_account.Sites.Count() == 0);            
            var post =
                         m_account.PrimarySite.Posts.Where(p => p.Media.Any(media => media.Type == MediumType.Audio)).First();

            var medium = post.Media.Where(media => media.Type == MediumType.Audio).First();            
            Assert.IsFalse(medium.Content.Length == 0);
            using (Stream mp3FileStream = File.Open(@"c:\Users\STEIN\Documents\mp3test.mpg", FileMode.Create, FileAccess.Write))
            {
                medium.Content.CopyToStream(mp3FileStream);
            }
        }


        [TestMethod]
        public void Assert_Exists_Post_In_Site()
        {
            m_account.LoadSites();            
            Assert.IsTrue(m_account.Sites.First().Posts.Count() > 0);
        }

        [TestMethod]
        public void Assert_Exists_Post_In_Page1_In_Site()
        {
            m_account.LoadSites();
            var posts = m_account.Sites.First().GetPostsInPage(1, 2);
            var posts2 = m_account.Sites.First().GetPostsInPage(1, 2);
            Assert.IsTrue(posts.Count() > 0);
            
        }

        [TestMethod]
        public void Assert_Exist_Tags_In_Site()
        {
            m_account.LoadSites();
            m_account.Sites.First().LoadTags();
            Assert.IsTrue(m_account.Sites.First().Tags.Count() > 0);

        }

        [TestMethod]
        public void Assert_Exist_Tagged_Posts()
        {
            m_account.LoadSites();
            m_account.Sites.First().GetPostsByTag("Všechno a nic");
            Assert.IsTrue(m_account.Sites.First().Posts.Count() > 0);

        }

        [TestMethod]
        public void Assert_Add_20_Posts()
        {
            for (int i = 0; i < 20; i++)
            {
                Assert_Add_Post_Without_Medium();
            }
        }
        [TestMethod]
        public void Assert_Add_Post_Without_Medium()
        {
            m_account.LoadSites();
           IPosterousPost newPost = m_account.Sites.First().CreatePost(DateTime.Now + " Příliš žluťoučký kůň úpěl ďábelské ódy",
                                               "Příliš žluťoučký kůň úpěl ďábelské ódy", true);
            
            newPost.AddTag("Všechno a nic");
            newPost.AddTag("C# omnia vincit");
            newPost.SaveChanges();
            Assert.IsTrue(newPost.Id > 0);

        }

        [TestMethod]
        public void Assert_Add_Post_With_Medium()
        {
            m_account.LoadSites();
            IPosterousPost newPost = m_account.Sites.First().CreatePost("Obrázek HUDBA TeST",
                                                "Příliš žluťoučký kůň úpěl ďábelské ódy", true);

            newPost.AppendMedium(@"c:\Users\STEIN\Documents\Hudba\Once\AlbumArtSmall.jpg");
            newPost.AppendMedium(@"c:\Users\STEIN\Documents\Hudba\Once\01_falling_slowly.mp3");
            newPost.SaveChanges();
            Assert.IsTrue(newPost.Id > 0);

        }

        [TestMethod]
        public void Assert_Add_Post_With_Medium_Stream()
        {
            m_account.LoadSites();
            IPosterousPost newPost = m_account.Sites.First().CreatePost("Media stream",
                                                "TCARARARARA", true);

            var fstream = File.OpenRead(@"c:\Users\STEIN\Documents\Hudba\Once\AlbumArtSmall.jpg");
            newPost.AppendMedium(fstream, "mojefotka.jpg", stream => stream.Close());
            
            newPost.SaveChanges();
            Assert.IsTrue(newPost.Id > 0);

        }

        [TestMethod]
        public void Assert_Update_Post_Without_Medium()
        {
            
            m_account.Sites.First().LoadAllPosts();
            var post = m_account.Sites.First().Posts.Where(p => p.Tags.Any()).First();
            string updateText = "Updated " + DateTime.Now.ToString();
            post.Body = updateText;
            post.Title = updateText;
            post.SaveChanges();
            Assert.IsTrue(post.Id > 0);

        }

        [TestMethod]
        public void Assert_Update_Post_With_Medium()
        {
            
            
            var post = m_account.Sites.First().Posts.First();
            string updateText = "Updated " + DateTime.Now.ToString();
            post.AppendMedium(@"c:\Users\STEIN\Documents\Hudba\Dylan Bob - Time Out Of Mind\AlbumArt_{6DF0A444-4F68-489B-AFCF-A985B02166BB}_Large.jpg" );
            post.Body = updateText;
            post.Title = updateText;
            post.SaveChanges();
            Assert.IsTrue(post.Id > 0);

        }

        [TestMethod]
        public void Assert_Add_Comment_To_Post()
        {            
            var site = m_account.PrimarySite;            
            var post = site.Posts.Last();
            string updateText = "Updated Comment" + DateTime.Now.ToString();
            var comment = post.CreateNewComment(updateText);            
            post.SaveChanges();
            Assert.IsTrue(comment.Id > 0);

        }

        [TestMethod]
        public void Assert_Exist_BitLY_Post()
        {
            ISinglePostInfo posInfo = PosterousApplication.Current.BitLyService.GetSinglePost("Iel3");
            Assert.IsNotNull(posInfo);
        }

        [TestMethod]
        public void Assert_Exist_BitLY_Post_MEDIUM()
        {
            ISinglePostInfo posInfo = PosterousApplication.Current.BitLyService.GetSinglePost("Iel3");
            Assert.IsFalse(posInfo.Media.First().Content.Length == 0);
        }
        [TestMethod]
        public void Assert_Add_Twitter_Post_Without_Medium()
        {
            
            ITwitterPost newPost = m_twitterAccount.CreatePost(DateTime.Now + " Příliš žluťoučký kůň úpěl ďábelské ódy",
                                                               "Příliš žluťoučký kůň úpěl ďábelské ódy", true);

            newPost.SaveChanges();
            Assert.IsTrue(newPost.MediaId != String.Empty);

        }
        [TestMethod]
        public void Assert_Add_Twitter_Post_Only_Medium()
        {

            ITwitterPost newPost = m_twitterAccount.CreatePost(null,
                                                               null, true);

            newPost.AppendMedium(MEDIA_PATH);
            newPost.SaveChanges();
            Assert.IsTrue(newPost.MediaId != String.Empty);

        }

        [TestMethod]
        public void Assert_Add_Twitter_Post_Only_Medium_Stream()
        {

            ITwitterPost newPost = m_twitterAccount.CreatePost(null,
                                                               null, true);
            
            var fstream = File.OpenRead(@"c:\Users\STEIN\Documents\Hudba\Once\AlbumArtSmall.jpg");
            newPost.AppendMedium(fstream, "mojefotka.jpg", stream => stream.Close());
                        
            newPost.SaveChanges();
            Assert.IsTrue(newPost.MediaId != String.Empty);

        }
        [TestMethod]
        public void Assert_Exist_Single_Post_From_Twitter_Post()
        {

            
            ITwitterPost newPost = m_twitterAccount.CreatePost(DateTime.Now + " Příliš žluťoučký kůň úpěl ďábelské ódy",
                                                               "Příliš žluťoučký kůň úpěl ďábelské ódy", true);
            newPost.AppendMedium(MEDIA_PATH);
            newPost.SaveChanges();
            ISinglePostInfo singlePostInfo = newPost.GetPostInfo();            
            Assert.IsNotNull(singlePostInfo);
            

        }
    }
}
